package com.interview.a_多线程.c_停止线程.g_停止线程_最佳实践.a_传递中断;

/**
 * 传递中断
 * <p>
 * 优先选择：try catch InterruptException之后，抛出异常，除非必须自己消化异常
 */

public class StopThread implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();

        //执行0.5秒时中断
        Thread.sleep(3000);
        thread.interrupt();
    }

    @Override
    public void run() {
        int count = 0;
        System.out.println("Thread.currentThread().isInterrupted()---> " + Thread.currentThread().isInterrupted());
        try {
            //            迭代中存在阻塞情况
            while (count <= 1000) {
                if (count % 10 == 0) {
                    System.out.println(count + "  是10的倍数");
                }
                exceptionA();
                count++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("收到传递中断..............");
            System.out.println("保存日志。。。。。。");
        }

        System.out.println("任务运行结束...  当前 interrupt=" + Thread.currentThread().isInterrupted());
    }

    private void exceptionA() throws InterruptedException {
        Thread.sleep(100);
    }
}
